﻿using System;
using System.Globalization;
using System.IO;
using System.Windows.Data;
using System.Windows.Media.Imaging;

//see:  http://forums.silverlight.net/forums/p/133909/298857.aspx

namespace MVVMtutorialAdvanced.Converter
{
	public class ByteArrayToImageConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			try
			{
				var image = new BitmapImage();
				image.SetSource(new MemoryStream((byte[])value));
				return image;
			}
			catch (Exception)
			{
				return null;
			}
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}
}
